package com.xmut.xmutbackend.controller;

import com.xmut.xmutbackend.entity.Result;
import com.xmut.xmutbackend.entity.StudentStatistics;
import com.xmut.xmutbackend.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 学生统计信息控制器
 */
@RestController
@RequestMapping("/stats") // 去掉/api前缀，因为代理已经重写了路径
@CrossOrigin // 添加跨域支持
public class StudentStatisticsController {

    @Autowired
    private IStudentService studentService;

    /**
     * 获取学生各项统计数据
     * @return 学生统计数据
     */
    @GetMapping
    public Result<StudentStatistics> getStudentStatistics() {
        try {
            System.out.println("正在获取学生统计数据...");
            StudentStatistics statistics = studentService.getStudentStatistics();
            return Result.success(statistics);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("获取统计数据异常: " + e.getMessage());
        }
    }
    
    /**
     * 测试接口，直接返回固定数据
     * @return 测试数据
     */
    @GetMapping("/test")
    public Result<Map<String, Object>> testEndpoint() {
        System.out.println("测试接口被调用...");
        Map<String, Object> testData = new HashMap<>();
        testData.put("status", "API工作正常");
        testData.put("message", "这是一个测试响应");
        testData.put("timestamp", System.currentTimeMillis());
        return Result.success(testData);
    }
} 